import os
import sys
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtMultimedia import *
from PySide2.QtMultimediaWidgets import QVideoWidget

class PlayVideo(QWidget):

    def __init__(self):
        super(PlayVideo, self).__init__()
        #全局变量
        self.ispause=-1

        self.main_layout=QVBoxLayout()

        self.hs=QSplitter(Qt.Horizontal)
        self.play_media=QMediaPlayer(self)
        self.vw=QVideoWidget(self)
        self.vw.setMinimumWidth(500)
        self.vw.setMinimumHeight(500)

        # self.play_media.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0]))
        # self.vw.show()
        # self.play_media.play()
        # self._play()

        self.control_frame=QFrame()
        self.control_layout=QVBoxLayout()
        self.choose_layout=QHBoxLayout()
        self.choose_label=QLabel('Video:')
        self.choose_edit=QLineEdit()
        self.choose_btn=QPushButton('Path')
        self.choose_btn.clicked.connect(self._choose_path)
        self.choose_layout.addWidget(self.choose_label)
        self.choose_layout.addWidget(self.choose_edit)
        self.choose_layout.addWidget(self.choose_btn)
        self.play_btn=QPushButton('Play')
        self.play_btn.clicked.connect(self._play)
        self.play_btn.setMinimumHeight(50)
        self.pause_btn=QPushButton('Pause')
        self.pause_btn.clicked.connect(self._pause)
        self.control_layout.addLayout(self.choose_layout)
        self.control_layout.addWidget(self.play_btn)
        self.control_layout.addWidget(self.pause_btn)
        self.control_frame.setLayout(self.control_layout)
        # self.main_layout.addWidget(self.play_media)
        self.hs.addWidget(self.vw)

        self.hs.addWidget(self.control_frame)
        self.main_layout.addWidget(self.hs)
        # self.main_layout.addWidget(self.control_frame)
        self.setLayout(self.main_layout)

    def _choose_path(self):
        # video_path=QFileDialog.getOpenFileUrl()[0]
        video_path=QFileDialog.getOpenFileName()[0]
        print("video_path:",video_path)
        self.choose_edit.setText(video_path)

    def _play(self):
        print('start play')
        self.play_media.setVideoOutput(self.vw)
        # self.play_media.setMedia(QUrl.fromLocalFile(self.choose_edit.text()))
        self.play_media.setMedia(QUrl(self.choose_edit.text()))
        # self.play_media.setMedia(QUrl.fromLocalFile(r"D:\sofeware\ev\evout\shi1.mp4"))
        self.play_media.play()
        print('OK')

    def _pause(self):
        if self.ispause==1:
            self.play_media.pause()
        else:
            self.play_media.play()
        self.ispause*=-1


if __name__ == '__main__':
    app=QApplication(sys.argv)
    p=PlayVideo()
    p.show()
    sys.exit(app.exec_())
